package com.punai.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.ParseContentUtil;
import com.punai.customer.domain.CusGxChapter;
import com.punai.customer.domain.CusGxFee;
import com.punai.customer.domain.CusLinkSubpkConsume;
import com.punai.customer.domain.bo.CJFlRes;
import com.punai.customer.domain.bo.CusCategoryBo;
import com.punai.customer.domain.bo.FeeLinkFeatureAndCategoryResultBo;
import com.punai.customer.domain.vo.CusUpdateConsumeVo;
import com.punai.customer.mapper.*;
import com.punai.customer.service.ICusLinkSubpkConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标准费用项材机耗量Service业务层处理
 *
 * @author zhx
 * @date 2023-02-02
 */
@Service
public class CusLinkSubpkConsumeServiceImpl extends ServiceImpl<CusLinkSubpkConsumeMapper,CusLinkSubpkConsume> implements ICusLinkSubpkConsumeService
{
    @Autowired
    private CusLinkSubpkConsumeMapper cusLinkSubpkConsumeMapper;
    @Autowired
    private CusGxChapterMapper dmcGxChapterMapper;
    @Autowired
    private CusGxFeeMapper cusGxFeeMapper;
    @Autowired
    private CusGxChapterMapper cusGxChapterMapper;
    @Autowired
    private CusFeatureMapper cusFeatureMapper;
    @Autowired
    private CusLinkGxfeeCategoryMapper dmcLinkGxfeeCategoryMapper;
    @Autowired
    private CusGxFeatureConsumeMapper cusGxFeatureConsumeMapper;

    @Override
    public List<CJFlRes> listGxConsume(String gxId, Long companyId, String address) {

        List<CusCategoryBo> cusCategoryBos = dmcLinkGxfeeCategoryMapper.selectLinkDataAndEntityByAddress(companyId, gxId, address);
        List<CJFlRes> cjFlRes = new ArrayList<>();
        if (CollUtil.isNotEmpty(cusCategoryBos)) {
            for (CusCategoryBo cusCategoryBo : cusCategoryBos) {
                CJFlRes cjfl = new CJFlRes();
                cjfl.setLinkId(cusCategoryBo.getLinkId());
                cjfl.setName(cusCategoryBo.getCategoriesName());
                cjfl.setUnit(cusCategoryBo.getUnit());
                cjfl.setSpec(cusCategoryBo.getSpec());
                cjfl.setType(cusCategoryBo.getType());
                cjfl.setReferenceConsume(new BigDecimal(new DecimalFormat("#0.0000").format(cusCategoryBo.getReferenceConsume())));
                cjfl.setRealityConsume(cusCategoryBo.getRealityConsume());
                cjFlRes.add(cjfl);
            }
        }
        return cjFlRes;
    }

    @Override
    public List<CJFlRes> listConsume(String linkGxFeatureId,String address, Long companyId) {
        return cusLinkSubpkConsumeMapper.listConsume(linkGxFeatureId,address,companyId);
    }

    @Override
    public List<TreeBo> listTree(Long companyId,String specId) {
        List<CusGxChapter> dmcGxChapters = dmcGxChapterMapper.selectList(
                Wrappers.<CusGxChapter>lambdaQuery()
                        .eq(CusGxChapter::getCompanyId, companyId)
                        .eq(CusGxChapter::getSpecId,specId)
        );
        List<TreeBo> convert = TreeBo.convert(dmcGxChapters,specId);
//        if (CollUtil.isNotEmpty(convert)) {
//            for (TreeBo treeBo : convert) {
//                List<TreeBo> children = treeBo.getChildren();
//                for (TreeBo child : children) {
//                    child.setFbId(child.getId());
//                    setChildrenFbId(child);
//                }
//            }
//        }
        return convert;
    }

    @Override
    public TableDataInfo selectCusGxFeeList(CusGxFee dmcGxFee) {
        String chapterId = dmcGxFee.getChapterId();
        Long companyId = dmcGxFee.getCompanyId();
        String address = dmcGxFee.getAddress();
        List<String> chapterIds = new ArrayList<>();
        if (StrUtil.isNotEmpty(chapterId)) {
            chapterIds.add(chapterId);
            List<CusGxChapter> chapters = cusGxChapterMapper.selectChildrenById(chapterId, companyId);
            List<String> childrenIds = chapters.stream().map(CusGxChapter::getId).collect(Collectors.toList());
            chapterIds.addAll(childrenIds);
        }
        PageUtils.startPage();
        List<CusGxFee> gxFees = cusGxFeeMapper.selectDmcGxFeeList(dmcGxFee, chapterIds, companyId);
        if (CollUtil.isEmpty(gxFees)) {
            return new TableDataInfo(gxFees,0);
        }
        Long total = new PageInfo(gxFees).getTotal();

        List<FeeLinkFeatureAndCategoryResultBo> feeLinkFeatureAndCategoryResultBoList = new ArrayList<>();
        for (CusGxFee gx : gxFees) {
            FeeLinkFeatureAndCategoryResultBo resultBo = new FeeLinkFeatureAndCategoryResultBo();
            resultBo.setId(gx.getId());
            resultBo.setCompanyId(gx.getCompanyId());
            resultBo.setChapterId(gx.getChapterId());
            resultBo.setName(gx.getName());
            resultBo.setUnit(gx.getUnit());
            resultBo.setSuitableRange(gx.getSuitableRange());
            String content = gx.getContent();
            resultBo.setContent(ParseContentUtil.parseContent(content));
            resultBo.setCalcRule(gx.getCalcRule());
            resultBo.setFeatureConsumeState(gx.getFeatureConsumeState());
            feeLinkFeatureAndCategoryResultBoList.add(resultBo);
        }

        return new TableDataInfo(feeLinkFeatureAndCategoryResultBoList,total.intValue());
    }

    @Override
    public FeeLinkFeatureAndCategoryResultBo selectCusGxFeeById(String id, Long companyId, String address) {
        FeeLinkFeatureAndCategoryResultBo resultBo = new FeeLinkFeatureAndCategoryResultBo();
        // 基础信息
        CusGxFee gx = getById(id, companyId);
        resultBo.setId(gx.getId());
        resultBo.setCompanyId(gx.getCompanyId());
        resultBo.setChapterId(gx.getChapterId());
        resultBo.setName(gx.getName());
        resultBo.setUnit(gx.getUnit());

        // 材机分类关联
        List<CusCategoryBo> cusCategoryBos = dmcLinkGxfeeCategoryMapper.selectLinkDataAndEntityByAddress(companyId,id,address);
        List<CJFlRes> cjFlRes = new ArrayList<>();
        if (CollUtil.isNotEmpty(cusCategoryBos)) {
            for (CusCategoryBo cusCategoryBo : cusCategoryBos) {
                CJFlRes cjfl = new CJFlRes();
                cjfl.setCategoryId(cusCategoryBo.getId());
                cjfl.setLinkId(cusCategoryBo.getLinkId());
                cjfl.setName(cusCategoryBo.getCategoriesName());
                cjfl.setUnit(cusCategoryBo.getUnit());
                cjfl.setSpec(cusCategoryBo.getSpec());
                cjfl.setType(cusCategoryBo.getType());
                cjfl.setReferenceConsume(cusCategoryBo.getReferenceConsume());
                cjfl.setRealityConsume(cusCategoryBo.getRealityConsume());
                cjFlRes.add(cjfl);
            }
        }
        resultBo.setCjflList(cjFlRes);
        return resultBo;
    }

    @Override
    @Transactional
    public int updateCusGxFee(Long companyId,List<CusLinkSubpkConsume> cusLinkSubpkConsumeList) {
        for (CusLinkSubpkConsume cusLinkSubpkConsume : cusLinkSubpkConsumeList) {
            cusLinkSubpkConsumeMapper.updateConsumeById(cusLinkSubpkConsume.getId(),companyId,cusLinkSubpkConsume.getRealityConsume());
        }
        return 200;
    }

    @Override
    public int updateConsume(CusUpdateConsumeVo vo) {

        if(vo.getConsume().compareTo(new BigDecimal("1000000")) > -1){
            throw new RuntimeException("耗量应在1000000以内");
        }
        cusLinkSubpkConsumeMapper.updateConsume(vo);
        return 200;
    }

    @Override
    public int delConsume(String id, Long companyId) {
        cusLinkSubpkConsumeMapper.updateDelFlagIsTwo(id,companyId);
        return 200;
    }

    @Override
    @Transactional
    public List<CJFlRes> recoverConsume(String linkGxFeatureId,String gxId, Long companyId) {
        String address = cusGxFeatureConsumeMapper.selectAddressById(linkGxFeatureId, companyId);
        cusLinkSubpkConsumeMapper.updateDelFlagIsZero(linkGxFeatureId,gxId,companyId);
        return cusLinkSubpkConsumeMapper.listConsume(linkGxFeatureId,address,companyId);
    }


    private void setChildrenFbId(TreeBo child) {
        List<TreeBo> children = child.getChildren();
        String fbId = child.getFbId();
        if (CollUtil.isNotEmpty(children)) {
            for (TreeBo treeBo : children) {
                treeBo.setFbId(fbId);
                setChildrenFbId(treeBo);
            }
        }
    }

    private CusGxFee getById(String id, Long companyId) {
        CusGxFee dmcGxFee = cusGxFeeMapper.selectOne(
                Wrappers.<CusGxFee>lambdaQuery()
                        .eq(CusGxFee::getCompanyId, companyId)
                        .eq(CusGxFee::getId, id)
        );
        return dmcGxFee;
    }

}
